package com.example.blogsystem.controller;

import com.example.blogsystem.common.AjaxResult;
import com.example.blogsystem.common.UserSessionUtils;
import com.example.blogsystem.entity.ArticleInfo;
import com.example.blogsystem.entity.UserInfo;
import com.example.blogsystem.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/mybloglist")
    public AjaxResult getMyBlogList(HttpServletRequest request) {
        UserInfo userInfo = UserSessionUtils.getSessionUser(request);
        if (userInfo == null) {
            return AjaxResult.fail(-1, "非法请求");
        }
        List<ArticleInfo> articleInfos = articleService.getMyBlogList(userInfo.getId());
        return AjaxResult.success(articleInfos);
    }

    @RequestMapping("/delete")
    public AjaxResult deleteBlog(HttpServletRequest request, Integer id) {
        if (id == null && id <= 0) {
            return AjaxResult.fail(-1, "非法请求");
        }
        UserInfo userInfo = UserSessionUtils.getSessionUser(request);
        if (userInfo == null) {
            return AjaxResult.fail(-2, "非法请求");
        }
        return AjaxResult.success(articleService.deleteBlog(id, userInfo.getId()));
    }

    @RequestMapping("/detail")
    public AjaxResult getArtDetail(Integer id) {
        if (id == null && id < 1) {
            return AjaxResult.fail(-1, "非法参数");
        }
        return AjaxResult.success(articleService.getArtDetail(id));
    }

    @RequestMapping("/updatecount")
    public AjaxResult updateRCount(Integer id) {
        if (id == null && id < 1) {
            return AjaxResult.fail(-1, "非法参数");
        }

        return AjaxResult.success(articleService.updateRCount(id));
    }

    @RequestMapping("/add")
    public AjaxResult addBlog(HttpServletRequest request, ArticleInfo articleInfo) throws IOException {
        // 1.非空效验
        if (articleInfo == null || !StringUtils.hasLength(articleInfo .getTitle()) ||
                !StringUtils.hasLength(articleInfo .getContent())) {
            // 非法参数
            return AjaxResult.fail(-1, "非法参数");
        }
        // 2.数据库添加操作
        // a.得到当前登录用户的 uid
        UserInfo userinfo = UserSessionUtils.getSessionUser(request);
        if (userinfo == null || userinfo.getId() <= 0) {
            // 无效的登录用户
            return AjaxResult.fail(-2, "无效的登录用户");
        }
        articleInfo.setUid(userinfo.getId());
        // b.添加数据库并返回结果
        return AjaxResult.success(articleService.addBlog(articleInfo));
    }

    @RequestMapping("/update")
    public AjaxResult update(HttpServletRequest request, ArticleInfo articleInfo) {
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle())
                || !StringUtils.hasLength(articleInfo.getContent()) || articleInfo.getId() == null) {
            return AjaxResult.fail(-1, "非法参数");
        }
        UserInfo userInfo = UserSessionUtils.getSessionUser(request);
        if (userInfo == null || userInfo.getId() < 1) {
            return AjaxResult.fail(-2, "非法参数");
        }
        // 核心! -> 确定改的是哪个人的文章
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdateTime(LocalDateTime.now());
        return AjaxResult.success(articleService.updateBlog(articleInfo));
    }

    /**
     * 分页
     * 页码从1开始
     *
     * @return
     */
    @RequestMapping("/listbypage")
    public AjaxResult getListByPage(Integer pindex, Integer psize) {
        if (pindex == null || pindex <= 1) {
            pindex = 1;
        }
        if (psize == null || psize < 1) {
            psize = 5;
        }
        // (页码-1)*页面显示的文章数
        int offsize = (pindex - 1) * psize;
        List<ArticleInfo> list = articleService.getListArtByPage(psize, offsize);
        int artCount = articleService.getACount();
        System.out.println(" artCount  = " + artCount);
        System.out.println("psize = " + psize);
        int pages = (int) Math.ceil(artCount / (psize * 1.0));
        System.out.println("pages" + pages);
        HashMap<String, Object> hashMap = new LinkedHashMap<>();
        hashMap.put("list", list);
        hashMap.put("pages", pages);
        return AjaxResult.success(hashMap);
    }
}
